#include <sys/time.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stddef.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <poll.h>
#include <sys/epoll.h>
#include <sys/select.h>
#include <dirent.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "InCommon.h"
#include "SdkMediaInit.h"
#include "InPerLed.h"
#include "InSdkSignalFunc.h"
#include "InSdkCallbackFunction.h"
#include "InPerKey.h"
#include "InSdkDevInfo.h"
#include "InSdkIniConfig.h"
#include "InSdkSaveLastDate.h"
#include "InSdkSoundPlay.h"
#include "InSdkSpkCtrl.h"
#include "InSdkRebootEveryday.h"
#include "AppRtmpInit.h"

#if CMAKE_SUPPORT_STEP_MOTOR
#include "InPerSteppingMotor.h"
#endif

#define DEF_TAG         "Main"

#define IPC_APP_STORAGE_PATH    "/mnt/config/"   //Path to save tuya sdk DB files, should be readable, writeable and storable
#define IPC_APP_UPGRADE_FILE    "/tmp/upgrade.tar" //File with path to download file during OTA
#define IPC_APP_SD_BASE_PATH    "/mnt/config/record/"      //SD card mount directory

Com_u8 g_ComThread_Running;
Com_u32 g_ComThread_Num = 0;
char IPC_APP_UUID[DEF_COM_UUID_LEN];
char IPC_APP_AUTHKEY[DEF_COM_KEY_LEN];
char IPC_APP_PID[DEF_COM_PID_LEN];
char IPC_APP_VERSION[DEF_COM_VERSION_LEN];

Com_s32 Sdk_GetDev_AllInfo(void)
{
    Com_s32 ret = DEF_COM_FAILED;

    memset(IPC_APP_PID,0,sizeof(IPC_APP_PID));
    memset(IPC_APP_UUID,0,sizeof(IPC_APP_UUID));
    memset(IPC_APP_AUTHKEY,0,sizeof(IPC_APP_AUTHKEY));
    memset(IPC_APP_VERSION,0,sizeof(IPC_APP_VERSION));

    /* get device UUID */
    ret = Ext_InSdkGetInfo_UUID(IPC_APP_UUID);
    if(ret != DEF_COM_SUCCESS)
    {
        PRT_ERR(DEF_TAG,"get uuid failed\n");
        return DEF_COM_FAILED;
    }

    /*get device AUTHKEY */
    ret = Ext_InSdkGetInfo_Authkey(IPC_APP_AUTHKEY);
    if(ret != DEF_COM_SUCCESS)
    {
        PRT_ERR(DEF_TAG,"get authkey failed\n");
        return DEF_COM_FAILED;
    }

    /*get device PID */
    ret = Ext_InSdkGetInfo_Pid(IPC_APP_PID);
    if(ret != DEF_COM_SUCCESS)
    {
        PRT_ERR(DEF_TAG,"get pid failed\n");
        return DEF_COM_FAILED;
    }

    /* get firmware version */
    ret = Ext_InSdkGetInfo_AllVer(IPC_APP_VERSION);
    if(ret != DEF_COM_SUCCESS)
    {
        PRT_ERR(DEF_TAG,"get firmware version failed\n");
        return DEF_COM_FAILED;
    }

    return DEF_COM_SUCCESS;
}

int main(void)
{
    g_ComThread_Running = DEF_COM_TRUE;

    Com_s32 ret = DEF_COM_FAILED;

    Ext_InSdkIniCfg_LockInit();
    Ext_InSdkIniCfg_Init();
    Ext_SdkGpio_Init();
    /* get timezone */
    Ext_InComTimer_SetTZSec(Ext_InSdkBasicCfg_GetVal(BASIC_CFG_TIMEZONE_SEC));
    Ext_InSdkSaveDate_Init();
    //Ext_InPer_Uart_Init();
    Ext_InSdk_Signal_Init();

#if CMAKE_SUPPORT_STEP_MOTOR
    Ext_InPer_StepMotor_Init();
#endif

    ret = Sdk_GetDev_AllInfo();
    if(ret != DEF_COM_SUCCESS)
    {
        PRT_ERR(DEF_TAG,"get dev info failed\n");
        return DEF_COM_FAILED;
    }

    Ext_InSdkSpkPlay_LockInit();
    Ext_InSdkSpkCtrl_Init();
    ret = Ext_Sdk_Media_Init();
    if(ret != DEF_COM_SUCCESS)
    {
        PRT_ERR(DEF_TAG,"Wrap media init failed !\n");
        return DEF_COM_FAILED;
    }

#if CMAKE_SUPPORT_STEEP_ENGINE
    Ext_InPer_SteerEngine_Init();
#endif

    Ext_InPer_Key_Init();
    Ext_InPerLed_Init();

    if (Ext_InComFile_Access(DEF_FLAG_NO_NETWORK) == DEF_COM_FILE_EXIST)
    {
        Com_s32 ini_ret = Ext_InSdkBasicCfg_GetVal(BASIC_CFG_NIGHT_MODE_SW);
        if (ini_ret == 0)
        {
            Ext_InComFile_JudgeCreat(ON_NIGHT_FILE_PATH);
            Ext_InComFile_Remove(OFF_NIGHT_FILE_PATH);
        }
        else if (ini_ret == 1)
        {
            Ext_InComFile_JudgeCreat(OFF_NIGHT_FILE_PATH);
            Ext_InComFile_Remove(ON_NIGHT_FILE_PATH);
        }
        else
        {
            Ext_InComFile_Remove(ON_NIGHT_FILE_PATH);
            Ext_InComFile_Remove(OFF_NIGHT_FILE_PATH);
        }
    }

    Ext_InSdkReboot_Init();
    Ext_AppRtmp_Init();

    while(g_ComThread_Running)
    {
        sleep(1);
    }

    while(g_ComThread_Num != 0)
    {
        PRT_WARN(DEF_TAG,"wait [%d] thread exit-----\n",g_ComThread_Num);
        usleep(100 * 1000);
    }

    ret = Ext_Sdk_Media_Exit();
    if(ret != DEF_COM_SUCCESS)
    {
        PRT_ERR(DEF_TAG,"Wrap media exit failed !\n");
    }

#if CMAKE_SUPPORT_STEP_MOTOR
    Ext_InPer_StepMotor_Destroy();
#endif

    Ext_InSdkSpkPlay_LockDestroy();
    Ext_InSdkIniCfg_LockDestroy();

    return 0;
}

